package com.jch.dm12_half_sync_half_async.example;

import java.sql.Connection;

public class SampleAlarmClient {

    private static final int ALARM_MSG_SUP_THRESHOLD = 10;
    static {
        AlarmMgr.getInstance().init();
    }

    public static void main(String[] args) {
        SampleAlarmClient alarmClient = new SampleAlarmClient();
        Connection dbConn = null;
        try{
            dbConn = alarmClient.retrieveDBConnection();
        } catch (Exception e){
            final AlarmMgr alarmMgr = AlarmMgr.getInstance();
            // 告警被重复发送至告警模块的次数
            int duplicateSubmissionCount;
            String alarmId = "000000100000020";
            final String alarmExtraInfo = "operation=GetDBConnection;detail=Failed to get DB connection:" + e.getMessage();
            duplicateSubmissionCount = alarmMgr.sendAlarm(AlarmType.FAULT,alarmId,alarmExtraInfo);
            if (duplicateSubmissionCount < ALARM_MSG_SUP_THRESHOLD){
                System.err.println("Alarm[" + alarmId + "] raised,extraInfo:" + alarmExtraInfo);
            } else {
                if (duplicateSubmissionCount == ALARM_MSG_SUP_THRESHOLD){
                    System.err.println("Alarm[" + alarmId + "] was raised more than " + ALARM_MSG_SUP_THRESHOLD + " times,it will no longer be logged.");
                }
            }
            return;
        }
        alarmClient.doSomething(dbConn);
    }

    private Connection retrieveDBConnection() throws Exception{
        Connection conn = null;
        return conn;
    }

    private void doSomething(Connection conn){

    }
}
